package com.tianhui.consignortwofei.mvp.presenter;

import android.content.Context;

import com.fgs.common.CommonResponse;
import com.fgs.common.entity.CommonDataListInfo;
import com.fgs.common.progress.ExceptionHandle;
import com.fgs.common.progress.ObserverResponseListener;
import com.tianhui.consignortwofei.mvp.contract.SourcePathContract;
import com.tianhui.consignortwofei.mvp.model.SourcePathModel;
import com.tianhui.consignortwofei.mvp.model.enty.SourcePathInfo;

import java.util.List;
import java.util.Map;

/**
 * @author fgs
 * @date 2020/8/31
 * @description
 **/
public class SourcePathPresenter extends SourcePathContract.Presenter {
    private Context mContext;
    private SourcePathModel mModel;

    public SourcePathPresenter(Context context) {
        this.mContext = context;
        mModel = new SourcePathModel();
    }

    @Override
    public void requestSourceList(Map<String, String> map, boolean isDialog) {
        mModel.sourcePathListModel(mContext, map, isDialog, getView().bindLifecycle(), new ObserverResponseListener<CommonResponse<CommonDataListInfo>>() {
            @Override
            public void onNext(CommonResponse<CommonDataListInfo> commonResponse) {
                if (getView() != null) {
                    if (commonResponse.getCode() == 20000) {
                        CommonDataListInfo data = commonResponse.getData();
                        getView().setPageInfo(data.current, data.pages);
                        List<SourcePathInfo> list = data.records;
                        if (list != null && list.size() > 0) {
                            getView().showDataList(list);
                        } else {
                            getView().showEmptyView();
                        }
                    }
                }


            }

            @Override
            public void onError(ExceptionHandle.ResponseThrowable e) {
                if (getView() != null) {
                    getView().setMsg(e.message);
                    getView().stopRefresh();
                }
            }
        });
    }
}
